<template>
  <div class="modal-overlay" v-if="visible">
    <div class="modal-content prescription-modal">
      <div class="modal-header">
        <h2 class="modal-title">电子处方详情</h2>
        <button type="button" class="btn-close" @click="closeModal">
          <i class="fas fa-times"></i>
        </button>
      </div>
      <div class="modal-body">
        <div class="prescription-details" v-if="prescription">
          <!-- 患者基本信息 -->
          <div class="detail-section patient-info">
            <h3>患者基本信息</h3>
            <div class="patient-details">
              <div class="detail-grid">
                <div class="detail-item">
                  <div class="detail-label">姓名:</div>
                  <div class="detail-value">{{ prescription.patientName }}</div>
                </div>
                <div class="detail-item">
                  <div class="detail-label">性别:</div>
                  <div class="detail-value">{{ prescription.patientGender }}</div>
                </div>
                <div class="detail-item">
                  <div class="detail-label">年龄:</div>
                  <div class="detail-value">{{ prescription.patientAge }}岁</div>
                </div>
                <div class="detail-item">
                  <div class="detail-label">身份证号:</div>
                  <div class="detail-value">{{ maskedIdCard }}</div>
                </div>
              </div>
              <div class="avatar-placeholder">
                <i class="fas fa-user-circle"></i>
                <span>患者头像</span>
              </div>
            </div>
          </div>

          <!-- 医生信息 -->
          <div class="detail-section doctor-info">
            <h3>医生信息</h3>
            <div class="doctor-details">
              <div class="doctor-name">
                <i class="fas fa-user-md"></i>
                {{ prescription.doctorName }} 医生
              </div>
              <div class="doctor-department">
                {{ prescription.department }}
              </div>
              <div class="prescription-date">
                开具时间: {{ formatDate(prescription.createTime) }}
              </div>
              <div class="prescription-date" v-if="prescription.distributeTime">
                开药时间: {{ formatDate(prescription.distributeTime) }}
              </div>
            </div>
          </div>

          <!-- 病情说明 -->
          <div class="detail-section diagnosis-info">
            <h3>病情说明</h3>
            <div class="diagnosis-content">
              <p>{{ prescription.diagnosis }}</p>
              <div class="symptoms">
                <h4>主要症状:</h4>
                <ul>
                  <li v-for="symptom in prescription.symptoms" :key="symptom">
                    {{ symptom }}
                  </li>
                </ul>
              </div>
            </div>
          </div>

          <!-- 药品清单 -->
          <div class="detail-section medicines-info">
            <h3>药品清单</h3>
            <div class="medicines-table">
              <table>
                <thead>
                  <tr>
                    <th>药品名称</th>
                    <th>规格</th>
                    <th>用法用量</th>
                    <th>数量</th>
                  </tr>
                </thead>
                <tbody>
                  <tr v-for="medicine in prescription.medicines" :key="medicine.name">
                    <td>{{ medicine.name }}</td>
                    <td>{{ medicine.specification }}</td>
                    <td>{{ medicine.usage }}</td>
                    <td>{{ medicine.quantity }}</td>
                  </tr>
                </tbody>
              </table>
            </div>
          </div>

          <!-- 康复建议 -->
          <div class="detail-section suggestions-info">
            <h3>康复建议</h3>
            <div class="suggestions-content">
              <div class="suggestion-category">
                <h4><i class="fas fa-utensils"></i> 饮食建议</h4>
                <p>{{ prescription.suggestions.diet }}</p>
              </div>
              <div class="suggestion-category">
                <h4><i class="fas fa-running"></i> 运动建议</h4>
                <p>{{ prescription.suggestions.exercise }}</p>
              </div>
              <div class="suggestion-category">
                <h4><i class="fas fa-bed"></i> 睡眠建议</h4>
                <p>{{ prescription.suggestions.sleep }}</p>
              </div>
              <div class="suggestion-category" v-if="prescription.suggestions.others">
                <h4><i class="fas fa-stethoscope"></i> 其他建议</h4>
                <p>{{ prescription.suggestions.others }}</p>
              </div>
            </div>
          </div>

          <!-- 医生签名 -->
          <div class="detail-section signature">
            <div class="signature-content">
              <div class="signature-line"></div>
              <div class="signature-text">
                {{ prescription.doctorName }} 医生签名
              </div>
            </div>
          </div>
        </div>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn-close-modal" @click="closeModal">
          关闭
        </button>
        <button
          type="button"
          class="btn-download-modal"
          @click="downloadPrescription"
        >
          <i class="fas fa-download"></i>
          下载处方
        </button>
      </div>
    </div>
  </div>
</template>

<script setup>
const props = defineProps({
  visible: {
    type: Boolean,
    required: true
  },
  prescription: {
    type: Object,
    default: null
  },
  maskedIdCard: {
    type: String,
    required: true
  }
})

const emit = defineEmits(['close', 'download'])

const closeModal = () => {
  emit('close')
}

const downloadPrescription = () => {
  emit('download', props.prescription)
}

const formatDate = (dateString) => {
  try {
    const date = new Date(dateString)
    return date.toLocaleDateString('zh-CN', {
      year: 'numeric',
      month: 'long',
      day: 'numeric',
      hour: '2-digit',
      minute: '2-digit'
    })
  } catch (error) {
    return '日期格式错误'
  }
}
</script>

<style scoped>
/* 模态框样式 */
.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
  padding: 20px;
}

.modal-content {
  background-color: white;
  border-radius: 16px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.3);
  max-width: 800px;
  width: 100%;
  max-height: 90vh;
  overflow-y: auto;
}

.prescription-modal {
  max-width: 900px;
}

.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 25px;
  border-bottom: 1px solid #f0f0f0;
}

.modal-title {
  font-size: 24px;
  font-weight: 700;
  color: #2c3e50;
  margin: 0;
}

.btn-close {
  background: none;
  border: none;
  font-size: 20px;
  color: #7f8c8d;
  cursor: pointer;
  padding: 5px;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.3s;
}

.btn-close:hover {
  color: #2c3e50;
}

.modal-body {
  padding: 25px;
}

.modal-footer {
  padding: 20px 25px;
  border-top: 1px solid #f0f0f0;
  display: flex;
  justify-content: flex-end;
  gap: 15px;
}

/* 处方详情样式 */
.prescription-details {
  padding: 10px 0;
}

.detail-section {
  margin-bottom: 30px;
  padding-bottom: 20px;
  border-bottom: 1px solid #f0f0f0;
}

.detail-section:last-of-type {
  border-bottom: none;
}

.detail-section h3 {
  font-size: 20px;
  font-weight: 700;
  color: #2c3e50;
  margin: 0 0 20px 0;
  padding-bottom: 10px;
  border-bottom: 2px solid #3498db;
}

/* 患者信息 */
.patient-details {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 30px;
}

.detail-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 15px;
  flex: 1;
}

.detail-item {
  display: flex;
}

.detail-label {
  min-width: 80px;
  font-weight: 600;
  color: #7f8c8d;
}

.detail-value {
  color: #2c3e50;
}

.avatar-placeholder {
  width: 100px;
  height: 120px;
  border: 2px dashed #ddd;
  border-radius: 8px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  color: #95a5a6;
  text-align: center;
}

.avatar-placeholder i {
  font-size: 40px;
  margin-bottom: 10px;
}

/* 医生信息 */
.doctor-details {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.doctor-name {
  font-size: 18px;
  font-weight: 600;
  color: #2c3e50;
  display: flex;
  align-items: center;
  gap: 10px;
}

.doctor-department {
  color: #7f8c8d;
}

.prescription-date {
  color: #7f8c8d;
  font-size: 14px;
}

/* 病情说明 */
.diagnosis-content p {
  font-size: 16px;
  line-height: 1.6;
  color: #2c3e50;
  margin-bottom: 15px;
}

.symptoms h4 {
  font-size: 16px;
  font-weight: 600;
  color: #2c3e50;
  margin-bottom: 10px;
}

.symptoms ul {
  margin: 0;
  padding-left: 20px;
}

.symptoms li {
  color: #7f8c8d;
  margin-bottom: 5px;
}

/* 药品清单 */
.medicines-table {
  overflow-x: auto;
}

.medicines-table table {
  width: 100%;
  border-collapse: collapse;
}

.medicines-table th,
.medicines-table td {
  padding: 12px 15px;
  text-align: left;
  border-bottom: 1px solid #f0f0f0;
}

.medicines-table th {
  background: #f8f9fa;
  font-weight: 600;
  color: #2c3e50;
}

.medicines-table tr:hover {
  background: #f8f9fa;
}

/* 康复建议 */
.suggestions-content {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
  gap: 20px;
}

.suggestion-category {
  background: #f8f9fa;
  padding: 15px;
  border-radius: 8px;
}

.suggestion-category h4 {
  font-size: 16px;
  font-weight: 600;
  color: #2c3e50;
  margin: 0 0 10px 0;
  display: flex;
  align-items: center;
  gap: 8px;
}

.suggestion-category p {
  font-size: 14px;
  color: #7f8c8d;
  line-height: 1.5;
  margin: 0;
}

/* 医生签名 */
.signature-content {
  text-align: right;
  padding-top: 20px;
}

.signature-line {
  width: 200px;
  height: 1px;
  background: #2c3e50;
  margin: 0 0 5px auto;
}

.signature-text {
  color: #7f8c8d;
  font-size: 14px;
}

/* 按钮样式 */
.btn-close-modal, .btn-download-modal {
  padding: 12px 24px;
  font-size: 16px;
  font-weight: 600;
  border: none;
  border-radius: 8px;
  cursor: pointer;
  transition: all 0.3s;
  display: flex;
  align-items: center;
  gap: 8px;
}

.btn-close-modal {
  background: #95a5a6;
  color: white;
}

.btn-download-modal {
  background: #2ecc71;
  color: white;
}

.btn-close-modal:hover, .btn-download-modal:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
}

.btn-close-modal:hover {
  background: #7f8c8d;
}

.btn-download-modal:hover {
  background: #27ae60;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .patient-details {
    flex-direction: column;
  }

  .detail-grid {
    grid-template-columns: 1fr;
  }

  .suggestions-content {
    grid-template-columns: 1fr;
  }

  .medicines-table {
    font-size: 14px;
  }

  .medicines-table th,
  .medicines-table td {
    padding: 8px 10px;
  }

  .modal-footer {
    flex-direction: column;
  }

  .modal-footer button {
    width: 100%;
  }
}
</style>
